package com.nodecollege.cloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.annotation.UserLoginAnnotation;
import com.nodecollege.cloud.common.model.LoginUser;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsFile;
import com.nodecollege.cloud.common.model.po.UpmsTenant;
import com.nodecollege.cloud.common.model.po.UpmsUser;
import com.nodecollege.cloud.common.utils.Base64Util;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.TenantService;
import com.nodecollege.cloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户中心
 *
 * @author LC
 * @date 2019/12/7 10:26
 */
@RestController
@UserLoginAnnotation
@RequestMapping("/userCenter")
public class UserCenterController {

    @Autowired
    private UserService userService;

    @Autowired
    private TenantService tenantService;

    /**
     * 注册企业（租户）
     *
     * @param upmsTenant
     * @return
     */
    @ApiAnnotation(modularName = "用户中心", description = "注册企业（租户）")
    @PostMapping("/registerTenant")
    public NCResult registerTenant(@RequestBody UpmsTenant upmsTenant) {
        upmsTenant.setCreateUserId(ThreadUserUtils.getLoginUser().getUserId());
        upmsTenant.setTenantId(null);
        tenantService.addTenant(upmsTenant);
        return NCResult.ok("添加租户成功");
    }

    @ApiAnnotation(modularName = "用户中心", description = "查询个人信息")
    @PostMapping("/getUserInfo")
    public NCResult<UpmsUser> getUserInfo() {
        return NCResult.ok(userService.getUserByUserId(ThreadUserUtils.getLoginUser().getUserId()));
    }

    @ApiAnnotation(modularName = "用户中心", description = "修改个人信息")
    @PostMapping("/updateUserInfo")
    public NCResult updateUserInfo(@RequestBody UpmsUser upmsUser) {
        upmsUser.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        userService.updateUser(upmsUser, null);
        return NCResult.ok();
    }

    /**
     * 查询个人的所有租户信息
     *
     * @param queryVO
     * @return
     */
    @ApiAnnotation(modularName = "用户中心", description = "查询个人的所有租户信息")
    @PostMapping("/getTenantList")
    public NCResult<UpmsTenant> getTenantList(@RequestBody QueryVO<Long> queryVO) {
        queryVO.setData(ThreadUserUtils.getLoginUser().getUserId());
        return tenantService.getTenantListByUserId(queryVO);
    }

    @ApiAnnotation(modularName = "用户中心", description = "设置并切换默认登陆企业")
    @PostMapping("/setDefaultTenant")
    public NCResult setDefaultTenant(@RequestBody LoginUser loginUser) {
        LoginUser login = ThreadUserUtils.getLoginUser();
        loginUser.setUserId(login.getUserId());
        loginUser.setAccessToken(login.getAccessToken());
        loginUser.setExpire(login.getExpire());
        userService.changeDefaultTenant(loginUser);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "用户中心", description = "上传头像")
    @PostMapping("/uploadAvatar")
    public NCResult<UpmsUser> uploadAvatar(@RequestBody JSONObject object) {
        LoginUser login = ThreadUserUtils.getLoginUser();
        MultipartFile avatarFile = Base64Util.base64ToMultipart(object.getString("avatar"));
        UpmsUser user = userService.uploadAvatar(avatarFile, login.getUserId());
        return NCResult.ok(user);
    }

}
